<template>
  <div>
    <el-form :model="formData" ref="form" :rules="rules" size="medium" v-loading="loading">
      <el-row :gutter="20">
        <el-col :span="12" :offset="0">
          <el-form-item label="保障部门审核意见" v-if="formData.ensure_check_msg">
            <el-input v-model="formData.ensure_check_msg" size="normal" type="textarea" :rows="4" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" :offset="0">
          <el-form-item label="中心内设部门审核意见">
            <el-input v-model="formData.internal_check_code" size="normal" type="textarea" :rows="4" clearable>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="本单位历史审核意见">
        <el-table :data="formData.list" border stripe height="300">
          <el-table-column type="index" label="序号" width="50">
          </el-table-column>
          <el-table-column prop="name" label="审核单位">
          </el-table-column>
          <el-table-column prop="message" label="审核信息">
          </el-table-column>
          <el-table-column prop="create_date" label="审核时间" width="100">
            <template slot-scope="{row}">
              {{$util.formatDate(row.create_date)}}
            </template>
          </el-table-column>
          <el-table-column prop="check_status" label="审核状态" width="80">
            <template slot-scope="{row}">
              {{row.check_status | f_checkStatus}}
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item label="中心内设部门审核意见">
        <el-input v-model="formData.message" size="normal" type="textarea" :rows="4" clearable></el-input>
      </el-form-item>
    </el-form>
    <div class="center">
      <el-button type="primary" size="default" @click="cancel" :loading="saveLoading">取消</el-button>
      <el-button type="danger" size="default" @click="reject" :loading="saveLoading">驳回</el-button>
      <el-button type="success" size="default" @click="submit" :loading="saveLoading">通过审核</el-button>
    </div>
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
import { f_checkStatus } from '@/utils/filters.js'
export default {
  name: 'Verify',
  components: {

  },
  props: {
    verifyParams: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      loading: false,
      saveLoading: false,
      formData: {},
      rules: {}
    }
  },
  created () {

  },
  mounted () {
    if (this.verifyParams.id) {
      this.init()
    }
  },
  methods: {
    async init () {
      this.loading = true
      const res = await this.$http('verifyLog', {
        id: this.verifyParams.id,
        status: this.verifyParams.status,
      })
      // 审核信息分离
      if (res.internal_check_code && res.internal_check_code.indexOf(this.user.company_name) >= 0) {
        let start = res.internal_check_code.indexOf(this.user.company_name) + this.user.company_name.length + 1
        let end = res.internal_check_code.substring(start).indexOf('\n') + start
        res.message = res.internal_check_code.substring(start, end)
      } else { // 未审核过的部门
        res.message = ''
      }
      this.formData = res
      this.loading = false
    },
    cancel () {
      this.saveLoading = false
      this.$emit('cancel');
    },
    reject () {
      this.$confirm('是否确认驳回?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.saveLoading = true
        this.reject1()
      }).catch(() => {

      });
    },
    async reject1 () {
      // 判断是内设部门还是保障部门下的项目
      let status = '2'
      const checkLog = await this.$http('verifyPower', {
        apply_batch: this.verifyParams.apply_batch,
        id: this.verifyParams.id
      })
      for (let item of checkLog) {
        if (item.company_name.indexOf('沈阳市信息中心-') > -1) {
          status = '1'
          break
        } else {
          status = '2'
        }
      }
      // 审核时间
      let date = new Date()
      let today = this.$util.formatDate(date, 'yyyy-MM-dd hh:mm:ss')
      let check_end_time = await this.$http('checkDate', {
        today: today,
        num: status.slice(0, 1)
      })
      // 字段处理
      let internal_check_code = ''
      // 内设部门是否审核过  -| 隔离本单位，|- 隔离内设部门
      if (this.verifyParams.internal_check_code) {
        // 本部门审核过处理，替换审核信息
        if (this.verifyParams.internal_check_code.indexOf(this.user.company_code) >= 0) {
          let start = this.verifyParams.internal_check_code.indexOf(this.user.company_code) + this.user.company_code.length + 2
          let end = this.verifyParams.internal_check_code.substring(start).indexOf('|-') + start
          internal_check_code = this.verifyParams.internal_check_code.replace(
            (this.user.company_code + '-|' + this.verifyParams.internal_check_code.substring(start, end)),
            (this.user.company_code + '-|' + this.formData.message)
          )
          // 本部门未审核过处理，累加审核信息
        } else if (this.verifyParams.internal_check_code.length > 1) {
          internal_check_code = this.verifyParams.internal_check_code + this.user.company_code + '-|' + this.formData.message + '|-'
        }
        // 未审核过直接添加
      } else {
        internal_check_code = this.user.company_code + '-|' + this.formData.message + '|-'
      }
      const res = await this.$http('inputProjectUp', {
        id: this.verifyParams.id,
        level: this.verifyParams.status,
        check_status: '2',
        internal_check_code: internal_check_code,
        status: status,
        check_end_time: check_end_time,
        log: {
          project_id: this.verifyParams.id,
          company_code: this.user.company_code,
          login_name: this.user.loginName,
          check_status: '2',
          status: this.verifyParams.status,
          target_status: status,
          message: this.formData.message,
        }
      })
      this.saveLoading = false
      if (res.errcode === 4000) {
        this.$tips.declareUp(res.errmsg)
      } else if (res.errcode === 9999) {
        this.$tips.error(res.errmsg)
      } else {
        this.$tips.success(res.errmsg)
      }

      this.$emit('cancel');
      this.$emit('init');
    },
    submit () {
      this.$confirm('是否确认上报?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.saveLoading = true
        this.submit1()
      }).catch(() => {
      });
    },
    async submit1 () {
      let param = {}
      const log = await this.$http('inputDisLog', {
        project_id: this.verifyParams.id
      })
      //  定义时间
      let date = new Date()
      let today = this.$util.formatDate(date, 'yyyy-MM-dd hh:mm:ss')
      for (let item of log.list) {
        /*  先判断是否有 4,51,52驳回
        */
        if (item.check_status === '2') {
          if (item.status === '52') {
            // 驳回到内设部门的，需要内设部门重新审核一遍
            if (item.target_status == '3') {
              param = await this.target3(today)
            } else {
              param.target_status = '4'
              param.check_end_time = await this.$http('checkDate', {
                today: today,
                num: '5'
              })
            }
            break
          } else if (item.status === '4') {
            param.target_status = '4'
            param.check_end_time = await this.$http('checkDate', {
              today: today,
              num: '4'
            })
            break
          } else if (item.status === '51') {
            // 驳回到内设部门的，需要内设部门重新审核一遍
            if (item.target_status == '3') {
              param = await this.target3(today)
            } else {
              param.target_status = '51'
              param.check_end_time = await this.$http('checkDate', {
                today: today,
                num: '5'
              })
            }
            break
          }
        } else {
          param = await this.target3(today)
          break
        }
      }
      const res = await this.$http('inputProjectUp', {
        id: this.verifyParams.id,
        level: this.verifyParams.status,
        apply_batch: this.verifyParams.apply_batch,
        check_status: '1',
        status: param.target_status,
        ...param,
        log: {
          project_id: this.verifyParams.id,
          company_code: this.user.company_code,
          login_name: this.user.loginName,
          check_status: '1',
          status: this.verifyParams.status,
          target_status: param.target_status,
          message: this.formData.message
        }
      })
      this.saveLoading = false
      if (res.errcode === 4000) {
        this.$tips.declareUp(res.errmsg)
      } else if (res.errcode === 9999) {
        this.$tips.error(res.errmsg)
      } else {
        this.$tips.success(res.errmsg)
      }

      this.$emit('cancel');
      this.$emit('init');
    },
    async target3 (today) {
      let param = {}
      /*  非驳回，在判断是否有内设机构审核code
             有的替换或者拼接，没有直接拼接
             internal_check_code  审核意见拼接  '-|'拼接部门和审核意见    '|-'拼接多个部门
             internal_code_log    审核是否同意拼接   ','
         */
      // 判断内设部门是否审核过
      if (this.verifyParams.internal_check_code) {
        // 判断本单位是否审核过
        if (this.verifyParams.internal_check_code.indexOf(this.user.company_code) >= 0) {
          let start = this.verifyParams.internal_check_code.indexOf(this.user.company_code) + this.user.company_code.length + 2
          let end = this.verifyParams.internal_check_code.substring(start).indexOf('|-') + start
          param.internal_check_code = this.verifyParams.internal_check_code.replace(
            (this.user.company_code + '-|' + this.verifyParams.internal_check_code.substring(start, end)),
            (this.user.company_code + '-|' + this.formData.message)
          )
        } else if (this.verifyParams.internal_check_code.length > 1) {
          param.internal_check_code = this.verifyParams.internal_check_code + this.user.company_code + '-|' + this.formData.message + '|-'
        }
        // 判断是否有审核记录
        if (this.verifyParams.internal_code_log) {
          param.internal_code_log = this.user.company_code + ',' + this.verifyParams.internal_code_log
        } else {
          param.internal_code_log = this.user.company_code
        }
      } else {
        param.internal_check_code = this.user.company_code + '-|' + this.formData.message + '|-'
        param.internal_code_log = this.user.company_code
      }
      // 判断是否联审完成,5个部门联评联审
      let index = param.internal_code_log.indexOf(',') // 字符出现的位置
      let num = 0 // 这个字符出现的次数
      while (index > 0) {
        num++ // 每出现一次 次数加一
        index = param.internal_code_log.indexOf(',', index + 1) // 从字符串出现的位置的下一位置开始继续查找
      }

      if (num === 4) {
        param.status = '51'
        param.check_end_time = await this.$http('checkDate', {
          today: today,
          num: '5'
        })
      } else {
        param.status = '3'
        param.check_end_time = await this.$http('checkDate', {
          today: today,
          num: '3'
        })
      }
      return param
    },
    check () {
      return
    }
  },
  computed: {
    ...mapGetters([
      'user'
    ])
  },
  filters: {
    f_checkStatus: f_checkStatus
  }
}
</script>
<style lang='scss' scoped>
</style>
